package com.fangyuzhong.intelliJ.hadoop.core.ui.list;

import com.fangyuzhong.intelliJ.hadoop.core.dispose.DisposableProjectComponent;
import com.fangyuzhong.intelliJ.hadoop.core.ui.FileSystemBaseFormImpl;
import com.intellij.openapi.util.Disposer;
import com.intellij.ui.AnActionButton;
import com.intellij.ui.AnActionButtonRunnable;
import com.intellij.ui.ToolbarDecorator;
import com.intellij.uiDesigner.core.GridConstraints;
import com.intellij.uiDesigner.core.GridLayoutManager;

import javax.swing.*;
import java.awt.*;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * Created by fangyuzhong on 17-7-21.
 */
public class EditableStringListForm
        extends FileSystemBaseFormImpl<DisposableProjectComponent>
{
    private JPanel component;
    private JLabel titleLabel;
    private JPanel listPanel;
    private EditableStringList editableStringList;

    public EditableStringListForm(DisposableProjectComponent parentComponent, String title, boolean sorted)
    {
        this(parentComponent, title, new ArrayList(), sorted);
    }

    public EditableStringListForm(DisposableProjectComponent parentComponent, String title, List<String> elements, boolean sorted)
    {
        super(parentComponent);
        this.editableStringList = new EditableStringList(null, elements, sorted, false);
        ToolbarDecorator decorator = ToolbarDecorator.createDecorator(this.editableStringList);
        decorator.setAddAction(new AnActionButtonRunnable()
        {
            public void run(AnActionButton anActionButton)
            {
                EditableStringListForm.this.editableStringList.insertRow();
            }
        });
        decorator.setRemoveAction(new AnActionButtonRunnable()
        {
            public void run(AnActionButton anActionButton)
            {
                EditableStringListForm.this.editableStringList.removeRow();
            }
        });
        decorator.setMoveUpAction(new AnActionButtonRunnable()
        {
            public void run(AnActionButton anActionButton)
            {
                EditableStringListForm.this.editableStringList.moveRowUp();
            }
        });
        decorator.setMoveDownAction(new AnActionButtonRunnable()
        {
            public void run(AnActionButton anActionButton)
            {
                EditableStringListForm.this.editableStringList.moveRowDown();
            }
        });
        this.titleLabel.setText(title);

        JPanel editableListPanel = decorator.createPanel();
        Container parent = this.editableStringList.getParent();
        parent.setBackground(this.editableStringList.getBackground());
        this.listPanel.add(editableListPanel, "Center");
        Disposer.register(this, this.editableStringList);
    }

    public JComponent getComponent()
    {
        return this.component;
    }

    public List<String> getStringValues()
    {
        return this.editableStringList.getStringValues();
    }

    public void setStringValues(Collection<String> stringValues)
    {
        this.editableStringList.setStringValues(stringValues);
    }

    {
// GUI initializer generated by IntelliJ IDEA GUI Designer
// >>> IMPORTANT!! <<<
// DO NOT EDIT OR ADD ANY CODE HERE!
        $$$setupUI$$$();
    }

    /**
     * Method generated by IntelliJ IDEA GUI Designer
     * >>> IMPORTANT!! <<<
     * DO NOT edit this method OR call it in your code!
     *
     * @noinspection ALL
     */
    private void $$$setupUI$$$()
    {
        component = new JPanel();
        component.setLayout(new GridLayoutManager(2, 1, new Insets(0, 0, 0, 0), -1, -1));
        titleLabel = new JLabel();
        titleLabel.setText("[title]");
        component.add(titleLabel, new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_NONE, GridConstraints.SIZEPOLICY_FIXED, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
        listPanel = new JPanel();
        listPanel.setLayout(new BorderLayout(0, 0));
        component.add(listPanel, new GridConstraints(1, 0, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_BOTH, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, null, null, null, 0, false));
    }

    /**
     * @noinspection ALL
     */
    public JComponent $$$getRootComponent$$$()
    {
        return component;
    }
}

